<?php

namespace Es\Struct;
/**
 * 索引结构体
 */
class IndexSettingsStruct 
{
    //分片数
    public $numberOfShards;
    //副本数
    public $numberOfReplicas;
    //索引数据的刷新时间
    public $refreshInterval;
    //读取数据的行数
    public $maxResultWindow;
    //索引选择
    public $analysis = [];

    public function __construct(
        int $numberOfShards   = 3,
        int $numberOfReplicas = 1,
        string $refreshInterval  = "1s",
        string $maxResultWindow  = "10000",
        array $analysis = []
    )
    {
        $this->numberOfShards   = $numberOfShards;
        $this->numberOfReplicas = $numberOfReplicas;
        $this->refreshInterval  = $refreshInterval;
        $this->maxResultWindow  = $maxResultWindow;
        $this->analysis         = $analysis;

    }

    public function asArray(): array
    {
        $settings = [
            "number_of_shards"   => $this->numberOfShards,
            "number_of_replicas" => $this->numberOfReplicas,
            "refresh_interval"   => $this->refreshInterval,
            "max_result_window"  => $this->maxResultWindow
        ];
        if(!empty($this->analysis)){
            $settings['analysis'] = $this->analysis;
        }
        return $settings;
    }
}
